package com.tianma315.admin.echarts.rest;

import com.alibaba.fastjson.JSON;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.Tooltip;
import com.github.abel533.echarts.axis.Axis;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Series;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.echarts.domain.EchartsVo;
import com.tianma315.admin.echarts.service.EchartsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts/json")
public class EchartsJsonController {

    @Resource
    private EchartsService echartsService;

    /**
     * 订单销售量
     * @param year
     * @return
     */
    @RequestMapping("/order/count")
    public Result orderCount(String year,String month){
        Map<Integer,String> datas=new HashMap<>();
        int size=12;
        List<EchartsVo> echarts=new ArrayList<>();
        if(month!=null&&!"".equals(month)){
           size=31;
           echarts= echartsService.selectOrderCountByMonth(year,month);
        }else{
            echarts= echartsService.selectOrderCountByYear(year);
        }
            for (int i = 1; i <=size ; i++) {
                if(!echarts.isEmpty()){
                    for (int j = 0; j < echarts.size(); j++) {
                        if (Integer.parseInt(echarts.get(j).getNames())==i) {
                            datas.put(Integer.parseInt(echarts.get(j).getNames()),echarts.get(j).getDatas());
                            break;
                        }else{
                            datas.put(i,"0");
                        }
                    }
                }else{
                    datas.put(i,"0");
                }
            }
        Option option = new Option();
        List<String> xAxisList=new ArrayList<>();
        for (Integer s : datas.keySet()) {
            xAxisList.add(month!=null && month.length()>0?s+"日":s+"月");
        }
        List<String> seriesList=new ArrayList<>();
        for (String value : datas.values()) {
            seriesList.add(value);
        }
//        if(year!=null) {
//            option.title(year + "年销售量变化趋势图");
//        }else{
//            option.title("销售量变化趋势图");
//        }
        option.legend("销售量统计");
        Tooltip tooltip=new Tooltip();
        //b:月份，c：数量
        tooltip.setFormatter("{a} {c}");
        option.setTooltip(tooltip);
        Axis axis = new CategoryAxis();
        axis.data(xAxisList.toArray());
        option.xAxis(axis);
        Axis y = new ValueAxis();
        y.setName("数量");
        option.yAxis(y);

        List<Series> series = new ArrayList<>();
        Series s = new Line() ;
        s.data(seriesList.toArray());
        s.setName("数量：");
        series.add(s);
        option.series(series);

        return Result.ok(JSON.parse(JSON.toJSONString(option)));
    }

    /**
     * 订单销售额
     * @param year
     * @return
     */
    @RequestMapping("/order/amount")
    public Result orderAmount(String year,String month){
        Map<Integer,String> datas=new HashMap<>();
        int size=12;
        List<EchartsVo> echarts=new ArrayList<>();
        if(month!=null&&!"".equals(month)){
            size=31;
            echarts= echartsService.selectOrderAmountByMonth(year,month);
        }else{
            echarts= echartsService.selectOrderAmountByYear(year);
        }
        for (int i = 1; i <=size ; i++) {
            if(!echarts.isEmpty()){
                for (int j = 0; j < echarts.size(); j++) {
                    if (Integer.parseInt(echarts.get(j).getNames())==i) {
                        datas.put(Integer.parseInt(echarts.get(j).getNames()),echarts.get(j).getDatas());
                        break;
                    }else{
                        datas.put(i,"0");
                    }
                }
            }else{
                datas.put(i,"0");
            }
        }
        Option option = new Option();
        List<String> xAxisList=new ArrayList<>();
        for (Integer s : datas.keySet()) {
            xAxisList.add(month!=null && month.length()>0?s+"日":s+"月");
        }
        List<String> seriesList=new ArrayList<>();
        for (String value : datas.values()) {
            seriesList.add(value);
        }
//        if(year!=null) {
//            option.title(year + "年销售量变化趋势图");
//        }else{
//            option.title("销售量变化趋势图");
//        }
        option.legend("销售额统计");
        Tooltip tooltip=new Tooltip();
        //b:月份，c：数量
        tooltip.setFormatter("{a} {c}");
        option.setTooltip(tooltip);
        Axis axis = new CategoryAxis();
        axis.data(xAxisList.toArray());
        option.xAxis(axis);
        Axis y = new ValueAxis();
        y.setName("金额");
        option.yAxis(y);

        List<Series> series = new ArrayList<>();
        Series s = new Line() ;
        s.data(seriesList.toArray());
        s.setName("金额：");
        series.add(s);
        option.series(series);
        return Result.ok(JSON.parse(JSON.toJSONString(option)));
    }

    @RequestMapping("/agent/province")
    public Result agentProvince(){

        Map<String,Object> result = new HashMap<>();
        result.put("min",0);
        result.put("max",0);

        List<EchartsVo> list=echartsService.selectAgentAreaCount();


        if (!list.isEmpty()){
            list.forEach(vo ->{
                if (Integer.parseInt(vo.getDatas())>Integer.parseInt(result.get("max").toString())){
                    result.put("max",vo.getDatas());
                }
            });
            result.put("list",list);
            return Result.ok(result);
        }else{
            result.put("list",list);
            return Result.ok(result);
        }

    }


    @RequestMapping("/test")
    Result test(){



        Option option = new Option();
        option.legend("销售额统计");
        option.title("aaa");
        Axis axis = new CategoryAxis();
        axis.data("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
        option.xAxis(axis);

        Axis y = new ValueAxis();
        option.yAxis(y);

        List<Series> series = new ArrayList<>();
        Series s = new Line() ;
        s.data(820, 932, 901, 934, 1290, 1330, 1320);
        series.add(s);
        option.series(series);

        return Result.ok(JSON.parse(JSON.toJSONString(option)));
    }



}
